import index from "../views/index.vue";
import cart from "../components/cart.vue";
import goodslist from "../components/goodslist.vue";
import goodslistslot from "../components/goodslistslot.vue";
import test from "../components/ljx_file.vue";
import chart from "../components/MyChart.vue";
import GoodSrelate from "../components/GoodSrelate.vue";
import { createRouter, createWebHistory } from "vue-router";
import { useStore } from "vuex";
const routes = [
  {
    path: "/",
    component: index,
    children: [
      { path: "goodslist", component: goodslist, meta: { title: "商品列表" } },
      {
        path: "goodslistslot",
        component: goodslistslot,
        meta: { title: "商品列表插槽" },
      },
      { path: "cart", component: cart, meta: { title: "我的购物车" } },
      { path: "chart", component: chart, meta: { title: "购物车图表统计" } },
      {
        path: "good-details",
        component: () => import("../components/GoodDetails.vue"),
        meta: { title: "商品详情" },
        children: [
          {
            path: "good-srelate",
            component: GoodSrelate,
            meta: { title: "产品相关系列" },
          },

        ],
      },
      {
        path: "good-srelate",
        component: () => import("../components/GoodSrelate.vue"),
        meta: { title: "产品相关系列" },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("../views/login.vue"),
    meta: { title: "用户登录" },
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});
//跳转之前进行判断，是否要求登录
router.beforeEach((to, from, next) => {
  // true  to.meta.requiresAuth 替换以下函数  this.$store.state.isLogin
  const store = useStore();
  let logged_in = store.getters.isLogin;
  if (!logged_in && to.matched.some((item) => {
    return item.meta.requiresAuth
  })) {
  next('/login');
} else {
  next();
}
});
//导航后守卫(跳转后)
router.afterEach((to, from) => {
  document.title = to.meta.title; //跳转后设置页面的title
});
export default router;
